package com.hot100;

import java.util.*;

public class Solution438 {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> res = new ArrayList<>();
        int[] hashP = new int[26];
        for (int i = 0; i < p.length(); i++) {
            hashP[p.charAt(i) - 'a']++;
        }
        int[] hashS = new int[26];
        int i = 0, j = 0;
        while (i < s.length()) {
            int curRight = s.charAt(i) - 'a';
            hashS[curRight]++;
            while (hashS[curRight] > hashP[curRight]) {
                int curLeft = s.charAt(j) - 'a';
                hashS[curLeft]--;
                j++;
            }
            if (i - j + 1 == p.length()) res.add(j);
            i++;
        }
        return res;
    }
}
